/*
 * @Descripttion: 
 * @Author: Silence
 * @Date: 2023-07-24 19:30:50
 * @LastEditors: Silence
 * @LastEditTime: 2023-07-24 21:23:09
 */
import Vue from 'vue'
import Vuex from 'vuex'

// 插件化的方式 注册vuex
Vue.use(Vuex)

const store = new Vuex.Store({
  state: {
    cart: wx.getStorageSync("cart") || []
  },
  mutations: {
    cn_changeCart(state, payload) {
      state.cart = payload
      wx.setStorage({
        key: "cart",
        data: payload
      })
    }
  },

  getters: {
    cn_CheckedGoodslist(state) {
      return state.cart.filter(item => item.goods_checked)
    },
    cn_CheckedGoodsCount(_, getters) {
      return getters.cn_CheckedGoodslist.reduce((prev, index) => {
        return prev += index.goods_number
      }, 0)
    },
    cn_CheckedGoodsPirce(_, getters) {
      return getters.cn_CheckedGoodslist.reduce((prev, index) => {
        return prev += index.goods_number * index.goods_price
      }, 0)
    }

  }

})

// commonjs ---  module.exports + require
// es6 module --- export + import
export default store